<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use News;
use Template;
use Ad;
use Menu;
class NewsController extends Controller
{
    public function getList(Request $request){
        try{

            if($request->has('uid')){
                $list=News::getList($request->input('uid'),5);
            }
            else{
                $list=News::getAllList(5);
            }
            $result=Template::get();
            $ad=Ad::posGet('news');
            $result['list']=$list;

            $result['ad']=$ad;
            $result['menu']=Menu::getCateMenu(3);
            $parent=Menu::getParentTree($request->input('uid'),3);
            $result['parent']=$parent;

            if($request->is('api/*')){
                return json_encode(
                    [
                        'ret'=>200,
                        'data'=>$result
                    ]
                );
            }
            else{

                return view('default.news' ,['articles' => $list])->with('result',$result)->with('list',$list)->with('page_name',$request->has('page_name')?$request->input('page_name'):'');
            }

        }
        catch (Exception $e){
            return json_encode(
                [
                    'ret'=>202,
                    'data'=>[
                        'status'=>0
                    ]
                ]
            );
        }

    }//获取新闻列表
    public function insert(Request $request){
        try{
            News::insert($request);
            return json_encode([
                'ret'=>200,
                'date'=>[
                    'status'=>1
                ]
            ]);
        }catch (Exception $e){
            return json_encode([
                'ret'=>202,
                'date'=>[
                    'status'=>0
                ]
            ]);
        }
    }//插入新闻
    public function detail(Request $request){
        try{
            $detail=News::detail($request->input('uid'));

            $result=Template::get();
            $result['detail']=$detail;
            $ad=Ad::posGet('news');

            $result['ad']=$ad;
            $result['menu']=Menu::getCateMenu(3);

            $parent=Menu::getParentTree($detail[0]->belone_menu_uid,3);
            $result['parent']=$parent;
            if($request->is('api/*')){
                return json_encode([
                    'ret'=>200,
                    'date'=>$result
                ]);
            }
            else{

                return view('default.news_detail')->with('result',$result)->with('page_name',$request->has('page_name')?$request->input('page_name'):'')->with('second','../');
            }
        }catch (Exception $e){
            return json_encode([
                'ret'=>202,
                'date'=>[]
            ]);
        }


    }//查看详情
}
